<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />

    <el-card shadow="always" :body-style="{ padding: '5px 10px 5px' }" class="mt10">
      <div slot="header" class="f16">
        <span style="font-weight: 600">{{ $t(`Print.MaterialOrderDetails`) }}</span>
      </div>
      <Table
        :tableHeader="tableHeader"
        :tableData="tableData"
        class="mt10"
        :isLoading="isLoading"
        :summaryData="summaryData"
        summary
        @sortChange="sortChange"
      />
      <PageModule :PageData="{ total }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, ref, watch } from 'vue'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'MaterialPurchaseInquiry'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import Find from '@/components/Find/index.vue'
import { selectList } from '@/plugins/mixin'
import fileRequest from '@/utils/fileRequest'
import service from '@/utils/request'
import { ElMessage } from 'element-plus'
import { debounce } from 'lodash'
const formList = ref([
  {
    type: 'select',
    selectValue: '',
    selectP: '材料类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '订购类型',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '请输入订购单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入型体',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料编码',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入材料颜色',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商代码 厂商名称',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '采购员',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '订购日期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '搜索',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '按查询导出明细'
  }
])
const tableHeader = ref([
  {
    label: '材料类型',
    prop: 'seqName'
  },
  {
    label: '订购类型',
    prop: 'purType'
  },
  {
    backgroundColor: true,
    label: '订购单号',
    prop: 'purNo'
  },
  {
    label: '采购员',
    prop: 'sysUser'
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    backgroundColor: true,
    label: '型体',
    prop: 'style'
  },
  {
    width: '120px',
    label: '料号',
    prop: 'mtlNo'
  },
  {
    label: '颜色',
    prop: 'mtlColor'
  },
  {
    width: '120px',
    label: '品名',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '订购日期',
    prop: 'purDate',
    sortable: 'custom'
  },
  {
    label: '厂商回复交期',
    prop: 'inDate'
  },
  {
    label: '需求交期',
    prop: 'vendorDate'
  },
  {
    label: '厂商',
    prop: 'vendorName'
  },
  {
    label: '订购数量',
    prop: 'purQty'
  },
  {
    label: '单价',
    prop: 'price'
  },
  {
    label: '金额',
    prop: 'amt'
  },
  {
    label: '入库数量',
    prop: 'qty'
  },
  {
    label: '入库日期',
    prop: 'recDate'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
// 表一模块
// '置空'按钮
const findClicked = async (content: string) => {
  if (content === '搜索') {
    getTableList()
  }
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
    getTableList()
  }
  if (content === '按查询导出明细') {
    const res = (await fileRequest.post(`/MaterialOrder/exportMpPurDis`, {
      lotNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
      )?.inputValue,
      mtlColor: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料颜色'
      )?.inputValue,
      mtlName: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
      )?.inputValue,
      mtlNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料编码'
      )?.inputValue,
      orderEndTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[1],
      orderStartTime: formList.value.find(
        (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
      )?.dateValue?.[0],
      purNo: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
      )?.inputValue,
      purType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '订购类型')
        ?.selectValue,
      seqName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '材料类型')
        ?.selectValue,
      sort: order.value, // 默认 1 降序 2 升序
      style: formList.value.find(
        (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
      )?.inputValue,
      sysUser: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '采购员')
        ?.selectValue,
      vendorName: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称')
        ?.selectValue?.split(' ')[1], // 厂商名称
      vendorNo: formList.value
        .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称')
        ?.selectValue?.split(' ')[0], // 厂商代码
      pageNum: currentPage.value,
      pageSize: pageSize.value
    })) as any
    const link = document.createElement('a') // 利用a标签自定义下载文件名
    let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
    link.style.display = 'none'
    //设置连接
    link.href = window.URL.createObjectURL(blob)
    const FileName = decodeURI(res.headers['content-disposition'].split(`filename=`)[1])
    link.download = FileName //自定义文件名字
    document.body.appendChild(link)
    //模拟点击事件
    link.click()
    document.body.removeChild(link)
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 总计
interface summaryDataType {
  index: number
  num: number | undefined
}
const summaryData = ref<summaryDataType[]>([
  {
    index: 18,
    num: 0
  },
  {
    index: 15,
    num: 0
  }
])
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}

const order = ref('')
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/MaterialOrder/getOrderDetail', {
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入指令'
    )?.inputValue,
    mtlColor: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料颜色'
    )?.inputValue,
    mtlName: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料名称'
    )?.inputValue,
    mtlNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入材料编码'
    )?.inputValue,
    orderEndTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[1],
    orderStartTime: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '订购日期'
    )?.dateValue?.[0],
    purNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入订购单号'
    )?.inputValue,
    purType: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '订购类型')
      ?.selectValue,
    seqName: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '材料类型')
      ?.selectValue,
    sort: order.value, // 默认 1 降序 2 升序
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '请输入型体'
    )?.inputValue,
    sysUser: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '采购员')
      ?.selectValue,
    vendorName: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称')
      ?.selectValue?.split(' ')[1], // 厂商名称
    vendorNo: formList.value
      .find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称')
      ?.selectValue?.split(' ')[0], // 厂商代码
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  console.log(
    formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '厂商代码 厂商名称')
      ?.selectValue
  )

  const table = tableDataList.materialOrderDetails.map((item: any) => {
    if (item.backgroundColor === 0) {
      item.backgroundColor = '#ffffff'
    } else if (item.backgroundColor === 1) {
      item.backgroundColor = '#00ff00'
    } else if (item.backgroundColor === 2) {
      item.backgroundColor = '#e0ff00'
    }
    return item
  })
  console.log(table)

  tableData.value = table
  total.value = tableDataList.total
  // currentRow()
  summaryData.value[0].num = tableDataList.amtCount
  summaryData.value[1].num = tableDataList.purQtyCount
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)

// 排序
const sortChange = (sort: string) => {
  if (sort === 'ascending') {
    console.log('升序')
    order.value = '1'
  } else if (sort === 'descending') {
    console.log('降序')
    order.value = '2'
  } else {
    console.log('取消排序')
    order.value = ''
  }
  getTableList()
}

const getSelectList = async () => {
  await selectList(formList, () => service.get('/MaterialOrder/getOrderCondition'))
}
onMounted(() => {
  getSelectList()
})
</script>

<style lang="less" scoped>
:deep(.el-form-item:nth-last-child(5)) {
  .el-select {
    width: 150px;
    height: 28px;
  }
}

// 主体样式
.ProductionDynamicMain {
  width: 100%;
  height: 670px;
  margin-top: 20px;

  .ProductionDynamicMainCard {
    width: 100%;
    height: 100%;
    box-shadow: none;
  }
}

// 主体头
.ProductionDynamicMainCardHeader {
  width: 98%;
  height: 540px;
  margin: auto;
  margin-top: 10px;
  margin-bottom: 10px;
}

// 主体底
.ProductionDynamicMainCardBottom {
  width: 98%;
  height: 110px;
  margin: auto;
  display: flex;
  justify-content: space-between;
  margin-top: 80px;
}

.MainLeftCardH1 {
  font-size: 16px;
  margin-bottom: 10px;
}

// Input 输入框的样式
.MainRightMainPoInput {
  width: 88px;
  height: 25px;
}

:deep(.ProductionDynamicMainCardBottom .el-input__wrapper) {
  background-color: #fff5eb;
}

.save {
  color: black;
  border: 1px solid #e7e7e7;
  background: #e7e7e7;
}
</style>
